﻿// Copyright (c) egmkang wang. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace XServer.Record
{
    using System;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using XServer.SDK;
    using Dapper;
    using XServer.Message;


    public sealed partial class MysqlUtils
    {
        public static async Task ClearRankItem(long server, int rank_type)
        {
            using (var conn = GetConn())
            {
                await conn.ExecuteAsync($"update rank_player_details set v1=0 where server={server} and rank_type={rank_type}");
            }
        }

        public static async Task SaveRankItem(long server, int rank_type, RankItemInfo info)
        {
            using (var conn = GetConn())
            {
                await conn.ExecuteAsync(
                    $"insert into rank_player_details" +
                    "(server, rank_type, player_id, update_time, name, army_name, avatar, v1, v2, v3, v4)" +
                    " values " +
                    $"({server}, {rank_type}, @Uid, @UpdateTime, @Name, @ArmyName, @Avatar, @V1, @V2, @V3, @V4)" +
                    " ON DUPLICATE KEY UPDATE update_time=values(update_time), name=values(name)" +
                    ", army_name=values(army_name), avatar=values(avatar), v1=values(v1)" +
                    ", v2=values(v2), v3=values(v3), v4=values(v4)"
                    , info);
            }
        }

        public static async Task<IEnumerable<RankItemInfo>> LoadRankItem(long server, int rank_type, int max_count)
        {
            using (var conn = GetConn())
            {
                var sql = $"select * from rank_player_details where server={server} and rank_type=rank_type and v1 > 0 order by v1 desc, update_time asc limit {max_count}";
                var result = await conn.QueryAsync<RankItemInfo>(sql);
                return result;
            }
        }
    }
}
